import random
from django.db import models
from apps.member.activity.models import Activity, MemberActivity
from apps.member.models import Member
from apps.member.stores.models import Coupon
from utils.exceptions import CustomError


class BargainActivity(models.Model):
    activity = models.ForeignKey(Activity, verbose_name='活动信息', on_delete=models.PROTECT, related_name='bargain',
                                 blank=True)
    coupon_count = models.PositiveIntegerField(verbose_name='优惠券数量')
    remain_count = models.PositiveIntegerField(verbose_name='还剩优惠卷数量')
    old_price = models.FloatField(verbose_name='原价')
    new_price = models.FloatField(verbose_name='低价')
    min_bargain_count = models.PositiveSmallIntegerField(verbose_name='砍价最小次数')
    max_bargain_count = models.PositiveSmallIntegerField(verbose_name='砍价最大次数')
    valid_time = models.IntegerField(verbose_name='砍价有效期,多长时间内砍完')

    # coupon = models.ForeignKey(Coupon, verbose_name='奖券', on_delete=models.PROTECT)

    class Meta:
        db_table = 'activity_bargain'
        verbose_name = '砍价活动'


class BargainActivityApply(models.Model):
    member_activity = models.ForeignKey(MemberActivity, verbose_name='用户活动', on_delete=models.PROTECT,
                                        related_name='member_bargain_apply', blank=True)
    bargain_count = models.PositiveSmallIntegerField(verbose_name='完成砍价次数')
    # name = models.CharField(max_length=10, verbose_name='名字')
    # mobile = models.CharField(max_length=11, verbose_name='电话')
    has_bargain_count = models.PositiveSmallIntegerField(verbose_name='已经砍价次数', default=0)
    bargain_arr = models.CharField(max_length=1000, verbose_name='砍价数组')
    has_bargain_amount = models.FloatField(verbose_name='已砍金额', default=0)
    remain_amount = models.FloatField(verbose_name='还剩金额')
    end_time = models.DateTimeField(verbose_name='砍价结束时间')
    end_task_id = models.CharField(max_length=100, verbose_name='活动结束celery任务ID', null=True, blank=True)

    class Meta:
        db_table = 'activity_bargain_apply'
        verbose_name = '砍价活动报名信息'

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的BargainActivityApply_id')

        instance = BargainActivityApply.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的砍价活动报名信息实例')
        return instance

    @staticmethod
    def hb_generator(hongbao_num, money_sum):
        '''砍价列表'''
        hongbao_remain = hongbao_num
        for i in range(hongbao_num - 1):
            min = 0.01
            max = money_sum / hongbao_remain * 2
            money = round(random.uniform(0, max), 2)
            hongbao_money = money if money > min else min
            hongbao_remain -= 1
            money_sum -= hongbao_money
            yield hongbao_money
        yield round(money_sum, 2)


class MemberBargainRecord(models.Model):
    bargain_activity = models.ForeignKey(BargainActivityApply, verbose_name='活动信息', on_delete=models.PROTECT,
                                         related_name='member_bargain_record')
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT)
    amount = models.FloatField(verbose_name='砍掉金额')
    create_time = models.DateTimeField(verbose_name='砍价时间', auto_now_add=True)

    class Meta:
        db_table = 'member_bargain_record'
        verbose_name = '用户砍价记录',
        ordering = ('-create_time', '-amount')

    @staticmethod
    def is_bargain(member, bargain_activity):
        member_bargain_record = MemberBargainRecord.objects.filter(member=member, bargain_activity=bargain_activity)
        if member_bargain_record:
            raise CustomError('你已经帮ta砍过了')
